package com.sport.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sport.entity.SysMenu;
import com.sport.entity.SysPermission;
import com.sport.entity.SysRole;
import com.sport.mapper.SysMenuMapper;
import com.sport.mapper.SysPermissionMapper;
import com.sport.mapper.SysRoleMapper;
import com.sport.service.SysMenuService;
import com.sport.service.SysRoleService;
import com.sport.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Permission;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    SysPermissionMapper sysPermissionMapper;

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    RedisUtil redisUtil;



    @Override
    public Result findPage(QueryInfo queryInfo) {
        log.info("开始角色分页:--->页面{},--->页数{},查询内容{}", queryInfo.getPageNumber(), queryInfo.getPageSize(), queryInfo.getQueryString());
        PageHelper.startPage(queryInfo.getPageNumber(), queryInfo.getPageSize());
        Page<SysRole> page = sysRoleMapper.findPage(queryInfo.getQueryString());
        long total = page.getTotal();
        List<SysRole> result = page.getResult();
        log.info("查询的总条数---->{}", total);
        log.info("分页列表——-{}", result);
        for (SysRole role : result) {
            List<SysMenu> menus=sysMenuMapper.findByRoleId(role.getId());
            for(SysMenu menu:menus){
                List<SysMenu> children=sysMenuMapper.findByRoleIdAndParentId(menu.getId(),role.getId());
                menu.setChildren(children);
            }
            role.setMenus(menus);
//            查询角色权限信息
            List<SysPermission> permissions=sysPermissionMapper.findByRoleId(role.getId());
            role.setPermissions(permissions);

        }
        /*
        result.forEach(item->{
            List<SysMenu> menus=sysMenuMapper.findByRoleId(item.getId());
            menus.forEach(menu->{
                List<SysMenu> children=sysMenuMapper.findByRoleIdAndParentId(menu.getId(),item.getId());
                menu.setChildren(children);
            });
            item.setMenus(menus);
            List<SysPermission> permissions=sysPermissionMapper.findByRoleId(role.getId());
            item.setPermissions(permissions);

        });
*/


        return Result.success("查询角色成功",result);
    }
    @Transactional
    @Override
    public Result insert(SysRole sysRole) {

log.info("根据角色标签名查询是否存在角色");

SysRole role=sysRoleMapper.findByLabel(sysRole.getLabel());

if(null!=role){
    return Result.fail("该角色已经存在");
}

//插入角色信息,此处跟XML里面使用的主键自增会自动返回一个角色id,下面用得到
        sysRoleMapper.insert(sysRole);

        if(sysRole.getPermissions().size()>0) {
        log.info("添加对应的权限数据");
        sysRole.getPermissions().forEach(item->{sysRoleMapper.insertPermissions(sysRole.getId(),item.getId());});

        }

        if(sysRole.getMenus().size()>0){
            log.info("添加对应的权限数据");
            sysRole.getMenus().forEach(item->sysRoleMapper.insertMenus(sysRole.getId(),item.getId()));
        }

        return Result.success("添加角色信息成功");
    }

    @Override
    public Result delete(Long id) {

//       根据角色Id查询是否存在菜单信息
        List<SysMenu> menus=sysMenuMapper.findByRoleId(id);
        List<SysMenu> childrens=new ArrayList<>();
       menus.forEach(item->{
           childrens.add((SysMenu) sysMenuMapper.findByRoleIdAndParentId(item.getId(),id));
       });

        if(menus.size()>0 ||childrens.size()>0){
            return Result.fail("删除失败，该角色拥有菜单信息，请先删除对应的菜单信息");
        }if(sysPermissionMapper.findByRoleId(id).size()>0){
            return Result.fail("删除失败，该角色拥有权限信息，请先删除对应的权限信息");

        }
        sysRoleMapper.delete(id);

        redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("数据删除成功");
    }
    @Transactional
    @Override
    public Result update(SysRole sysRole) {
        sysRoleMapper.update(sysRole);
        if(sysRole.getPermissions().size()>0) {
            log.info("先删除对应的权限数据");
            sysRoleMapper.deletePermissionsByRoleId(sysRole.getId());
            log.info("再添加对应的权限数据");
            sysRole.getPermissions().forEach(item->{sysRoleMapper.insertPermissions(sysRole.getId(),item.getId());});

        }

        if(sysRole.getMenus().size()>0){
            log.info("先删除对应的菜单权限数据");
            sysRoleMapper.deleteMenusByRoleId(sysRole.getId());
            log.info("再添加对应的权限数据");
            sysRole.getMenus().forEach(item->sysRoleMapper.insertMenus(sysRole.getId(),item.getId()));
        }
        redisUtil.delKey("userInfo_"+ SecurityUtil.getUsername());
        return Result.success("数据修改成功");
    }

}

